//
//  ColorPickerViewController.m
//  BlueToothProject
//
//  Created by foshou on 2017/2/27.
//  Copyright © 2017年 yxw. All rights reserved.
//

#import "ColorPickerViewController.h"
#import "FSColorPicker.h"
#import "UIImage+ColorSelect.h"
#import "UIColor+RgbAndString.h"
#import "DiyHeader.h"
#import "KZColorPicker.h"

@interface ColorPickerViewController ()

@property (nonatomic,strong) KZColorPicker *colorPicker;

@end

#define PickerHeight (Main_Screen_Height-200)

@implementation ColorPickerViewController
{
    UIColor *currentColor;
    UIImageView *sourceImageView;
    UIView *resultView;
    UIView *brightnessSlider;
    
    UILabel *resultLab;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    //导航
    UIBarButtonItem *doneItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneChooseColor)];
    self.navigationItem.rightBarButtonItem = doneItem;
    
    currentColor = self.chooseColor;
    
//    CGPoint point = [[[NSUserDefaults standardUserDefaults] valueForKey:LastPointValue] CGPointValue];
//    if (point) {
//        
//    }

    //颜色区域
//    [self setColorPickerView];
    
    self.colorPicker = [[KZColorPicker alloc] initWithFrame:CGRectMake(0, 64, Main_Screen_Width, Main_Screen_Height-64)];
    _colorPicker.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    _colorPicker.selectedColor = self.chooseColor;
    _colorPicker.oldColor = self.chooseColor;
    [_colorPicker addTarget:self action:@selector(pickerChanged:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:_colorPicker];
    
    //结果展示
//    [self createSeleterResultView];
}

//-(void)setColorPickerView
//{
//    UIView *colorView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, Main_Screen_Width, PickerHeight)];
//    //draw wings
//    FSColorPicker *picker = [[FSColorPicker alloc]initWithFrame:colorView.bounds];
//    //    [colorView.layer addSublayer:picker.layer];
//    [colorView addSubview:picker];
//    
//    CAGradientLayer *gradientLayer2 = [CAGradientLayer layer];
//    gradientLayer2.colors = @[(__bridge id)[UIColor clearColor].CGColor,(__bridge id)[UIColor colorWithWhite:1 alpha:0.05].CGColor,(__bridge id)[UIColor whiteColor].CGColor];
//    gradientLayer2.locations = @[@0.01,@0.02, @1];
//    gradientLayer2.startPoint = CGPointMake(0, 0);
//    gradientLayer2.endPoint = CGPointMake(0, 1);
//    gradientLayer2.frame = colorView.bounds;
//    [colorView.layer addSublayer:gradientLayer2];
//    //    [self.view addSubview:colorView];
//    
//    UIImage *image = [self makeImageWithView:colorView withSize:colorView.bounds.size];
//    sourceImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 200, self.view.frame.size.width, PickerHeight)];
//    sourceImageView.image = image;
//    [self.view addSubview:sourceImageView];
//}
//
//-(void)createSeleterResultView
//{
//    //
//    CGFloat width = 66*MultipleWideth;
//    UIView *showView = [[UIView alloc]initWithFrame:CGRectMake(12, 80, width, 84*MultipleWideth)];
//    showView.layer.cornerRadius = 4.0;
//    showView.layer.borderColor = [[UIColor lightGrayColor] CGColor];
//    showView.layer.borderWidth = 0.5;
//    showView.layer.masksToBounds = YES;
//    resultView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, width, width)];
//    resultView.backgroundColor = self.chooseColor;
//    
//    [showView addSubview:resultView];
//    
//    resultLab = [[UILabel alloc]initWithFrame:CGRectMake(0, width, width, 18*MultipleWideth)];
//    resultLab.textColor = [UIColor darkGrayColor];
//    resultLab.font = [UIFont systemFontOfSize:12];
//    resultLab.textAlignment = NSTextAlignmentCenter;
//    [showView addSubview:resultLab];
//    [self.view addSubview:showView];
//    
//    brightnessSlider = [[UIView alloc]initWithFrame:CGRectMake(showView.right+20, 124*MultipleWideth, Main_Screen_Width-showView.right-40, 14*MultipleWideth)];
//    brightnessSlider.backgroundColor = self.chooseColor;
//    brightnessSlider.layer.cornerRadius = 14*MultipleWideth/2.0;
//    [self.view addSubview:brightnessSlider];
//}

- (void) pickerChanged:(KZColorPicker *)cp
{
    currentColor = cp.selectedColor;
//    [delegate defaultColorController:self didChangeColor:cp.selectedColor];
}

-(void)doneChooseColor
{
    self.chooseColor = currentColor;
    [self.navigationController popViewControllerAnimated:YES];
}

//完成选择
-(void)finishedAction:(CGPoint)point{
    UIColor *tempColor = [sourceImageView.image colorAtPixel:point];
    [resultView setBackgroundColor:tempColor];
    NSLog(@"\n_x=%f,y=%f",point.x,point.y);
    
    NSString *colorValue = [UIColor toStrByUIColor:tempColor];
    resultLab.text = [NSString stringWithFormat:@"#%@",colorValue];
}


//Touch
//- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
//    UITouch *touch = [touches anyObject];
//
//    CGPoint loc = [touch locationInView:self.view];
//    if (!CGRectContainsPoint(sourceImageView.frame, loc)) {
//        return;
//    }
//    CGPoint tempPoint = [touch locationInView:sourceImageView];
//    [self finishedAction:tempPoint];
//}
//
//- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
//    UITouch *touch = [touches anyObject];
//    CGPoint loc = [touch locationInView:self.view];
//    if (!CGRectContainsPoint(sourceImageView.frame, loc)) {
//        return;
//    }
//    CGPoint tempPoint = [touch locationInView:sourceImageView];
//    [self finishedAction:tempPoint];
//}
//
//- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
//    
//}

-(void)dealloc
{
    resultView = nil;
    sourceImageView = nil;
    brightnessSlider = nil;
    resultLab = nil;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
